package com.diy.sigmund.diycommon.test.controller.excel.easyexcel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.FileUtils;
import java.io.File;
import java.io.InputStream;

/**
 * @author ylm-sigmund
 * @since 2021/9/18 10:18
 */
@ContentRowHeight(100)
@ColumnWidth(100 / 8)
public class ImageData implements Converter<String> {

    private File file;
    private InputStream inputStream;
    @ExcelProperty(converter = ImageData.class)
    private String string;
    private byte[] byteArray;

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }

    public byte[] getByteArray() {
        return byteArray;
    }

    public void setByteArray(byte[] byteArray) {
        this.byteArray = byteArray;
    }

    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    // @Override
    // public CellDataTypeEnum supportExcelTypeKey() {
    //     return CellDataTypeEnum.IMAGE;
    // }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return Converter.super.supportExcelTypeKey();
    }

    // @Override
    // public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
    //         GlobalConfiguration globalConfiguration) {
    //     throw new UnsupportedOperationException("Cannot convert images to string");
    // }

    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
            GlobalConfiguration globalConfiguration) throws Exception {
        throw new UnsupportedOperationException("Cannot convert images to string");
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
            GlobalConfiguration globalConfiguration) throws Exception {
        return new WriteCellData(FileUtils.readFileToByteArray(new File(value)));
    }
}
